package com.banciyuan.bcywebview.biz.write;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.DailyApi;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.ProgressbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.write.write.WriteActivity;
import com.banciyuan.bcywebview.biz.write.write.WriteEditAdapter;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.DetailType;
import de.greenrobot.daoexample.model.PostItem;

/**
 * Created by hy on 2016/5/24.
 */
public class DailyEditActivity extends WriteActivity {
    private DetailType detailData;
    private RequestQueue mQueue;
    private PostItem postItem;
    private final static int NOEDIT = 4301;
    private ProgressbarHelper mProgressbarHelper;
    private WriteEditAdapter writeGridAdapter;

    @Override
    protected void initArgs() {
        super.initArgs();
        mQueue = VolleyQueue.getRquest(this);
        detailData = (DetailType) getIntent().getSerializableExtra("edit_data");
        can_drag=false;
    }

    @Override
    protected void initUi() {
        super.initUi();
        getEdit();
    }


    @Override
    protected void initProgressbar() {
        super.initProgressbar();
        View mProgressbarView = findViewById(R.id.base_progressbar);
        mProgressbarHelper = new ProgressbarHelper(mProgressbarView);
        mProgressbarHelper.setActionCallbacks(new ProgressbarHelper.OnProgressCallbacks() {
            @Override
            public void onActionClick(int id) {
                getEdit();
            }
        });
        mProgressbarHelper.onLoading();
    }

    @Override
    protected void initActionbar() {
        View mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.addItemOneText(getString(R.string.next));
        mActionbarHelper.setTitle(getString(R.string.edit_daily));
        mActionbarHelper.addBackText(getString(R.string.mydialog_cancel));
        mActionbarHelper.setActionCallbacks(this);
        tv_save = (TextView) findViewById(R.id.base_action_bar_home_text_item);
    }

    private void getEdit() {
        String url = HttpUtils.BASE_URL + DailyApi.getEditDaily();
        List<BcyPair> pairs = new ArrayList<BcyPair>();
        pairs.add(new BcyPair(HttpUtils.USER_TOKEN, UserDataHelper.getInstance(this).getToken()));
        pairs.add(new BcyPair(HttpUtils.UDID, detailData.getUd_id()));
        HashMap<String, String> map = HttpUtils.getData(pairs);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (StatusCodeUtils.dealStatusCode(s, DailyEditActivity.this)) {
                        String result = jsonObject.getString(HttpUtils.UPLOAD_DATA);
                        Gson gson = new Gson();
                        postItem = gson.fromJson(result, new TypeToken<PostItem>() {
                        }.getType());
                        renderData();
                    } else {
                        int status = jsonObject.getInt(HttpUtils.UPLOAD_STATUS);
                        if (status == NOEDIT) {
                            mProgressbarHelper.onNodata(getString(R.string.no_edit));
                        }
                    }
                } catch (Exception e) {
                    mProgressbarHelper.onFailed();
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                mProgressbarHelper.onFailed();
            }
        };
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onError);
        mQueue.add(requestParam);
    }

    private void renderData() {
        writeGridAdapter = new WriteEditAdapter(this,postItem.getMulti(),headerHolder);
        recyclerView.setAdapter(writeGridAdapter);
        headerHolder.editText.setText(postItem.getPlain());
        default_work = postItem.getWork();
        mAuthority = postItem.getAuthority();
        mProgressbarHelper.onSuccess();
    }

    @Override
    public void gotoWriteTag() {
        Intent intent = new Intent();
        intent.putExtra("content", headerHolder.editText.getText().toString().trim());
        intent.putExtra("plist", (Serializable) plist);
        intent.putExtra("work", default_work);
        intent.putExtra("tag", default_tag);
        intent.putExtra("authority", mAuthority);
        intent.putExtra("ud_id", detailData.getUd_id());
        intent.setClass(this, DailyEditTagActivity.class);
        startActivityForResult(intent, WRITE_ACTIVITY);
    }

}
